<template>
  <d-scroll-view  @refresherrefresh="getDetail" :refresher-triggered="triggered">
    <proj-detail-layout ref="projDetailLayout" :process-instance-id="processInstanceId" :project-number="projectNumber">
    <d-forms label-width="180rpx">
      <view>
        <text class="form-title-1">虚拟合同申请理由</text>
        <d-card>{{ formatStr(form?.virtualApplyReason) }}</d-card>
      </view>
      <d-forms-item label="销售经理" :input="form?.salesManagerStr"></d-forms-item>
      <d-forms-item label="地址" :input="form?.address"></d-forms-item>
      <d-forms-item label="合同名称" :input="form?.contractName"></d-forms-item>
      <d-forms-item label="合同编号" :input="form?.contractNumber"></d-forms-item>
      <d-forms-item label="签约时间" :type="DFormInputType.DATE" :input="form.signingTime" format="yyyy-MM-dd"></d-forms-item>
      <d-forms-item label="签约客户名称" :input="form?.signCustomerNameName"></d-forms-item>
      <d-forms-item label="签约客户联系人" :input="form?.signCustomerContactsName"></d-forms-item>
      <d-forms-item label="签约客户联系人电话" :input="form?.signCustomerPhone"></d-forms-item>
      <d-forms-item label="合同起始日期" :type="DFormInputType.DATE" :input="form.startTime" format="yyyy-MM-dd"></d-forms-item>
      <d-forms-item label="合同截止日期" :type="DFormInputType.DATE" :input="form.endTime" format="yyyy-MM-dd"></d-forms-item>
      <d-forms-item label="预计质保到期时间" :type="DFormInputType.DATE" :input="form.warrantyTime" format="yyyy-MM-dd"></d-forms-item>
      <d-forms-item label="含税合同金额" :type="DFormInputType.AMOUNT" :input="form?.amountIncludingTax"></d-forms-item>
      <d-forms-item label="不含税合同金额" :type="DFormInputType.AMOUNT" :input="form?.amountExcludingTax"></d-forms-item>
      <d-forms-item v-if="form.taxRateType === PmContractTaxType.SINGLE" label="税率" :input="form?.taxRate" suffix="%"></d-forms-item>
      <view v-else-if="form.taxRateType === PmContractTaxType.VARIOUS">
        <text class="form-title-1">税率（{{ getTaxRateType(form?.taxRateType) }}）</text>
        <view v-if="form.taxRateList">
          <d-card v-for="(tax, idx) in form.taxRateList" :key="'tax_'+idx" style="margin-bottom: 20rpx">
            <d-forms disabled label-width="180rpx">
              <d-forms-item label="税率" :input="tax.taxRate" suffix="%"></d-forms-item>
              <d-forms-item label="含税合同金额" :type="DFormInputType.AMOUNT" :input="tax.amountIncludingTax"></d-forms-item>
              <d-forms-item label="不含税合同金额" :type="DFormInputType.AMOUNT" :input="tax.amountExcludingTax"></d-forms-item>
            </d-forms>
          </d-card>
        </view>
      </view>
      <view>
        <text class="form-title-1">合同电子版</text>
        <d-card>
          <pm-file-upload readonly :files="form.attachUrl"></pm-file-upload>
        </d-card>
      </view>
      <view>
        <text class="form-title-1">合同扫描版</text>
        <d-card>
          <pm-file-upload readonly :files="form.pdfAttachUrl"></pm-file-upload>
        </d-card>
      </view>
      <view>
        <text class="form-title-1">变更/补充 附件</text>
        <d-card>
          <pm-file-upload readonly :files="form.changeAttachUrl"></pm-file-upload>
        </d-card>
      </view>
      <view>
        <text class="form-title-1">计划回款周期</text>
        <planned-payment-info :list="form.plannedPaymentList"></planned-payment-info>
      </view>
      <view>
        <text class="form-title-1">履约保证金</text>
        <performance-bond :list="form.keepPromiseMoneyList"></performance-bond>
      </view>
      <view>
        <text class="form-title-1">质保金</text>
        <warranty-deposits :list="form.qualityGuaranteMoneyList"></warranty-deposits>
      </view>
    </d-forms>
  </proj-detail-layout>
  </d-scroll-view>
</template>

<script setup lang="ts">
import ProjDetailLayout from "@/components/ProjDetailLayout/index.vue";
import {useContractSignApi} from "@/api/pm/project/contractSign";
import {DFormInputType, PmContractTaxType} from "@/utils/enum";
import PlannedPaymentInfo
  from "@/views/project/projectDetails/deliveryPhase/projectContractSigning/components/plannedPaymentInfo/index.vue";
import PerformanceBond
  from "@/views/project/projectDetails/deliveryPhase/projectContractSigning/components/performanceBond/index.vue";
import WarrantyDeposits
  from "@/views/project/projectDetails/deliveryPhase/projectContractSigning/components/warrantyDeposit/index.vue";
import {formatStr} from "@/utils";

defineOptions({ name: 'VirtualContract' });

const initForm = {
  taxRate: null,
  taxRateType: null,
  taxRateList: null,
  projectNumber: null,
  signCustomerPhone: null,
  signingTime: null,
  signCustomerContacts: null,
  signCustomerContactsName: null,
  startTime: null,
  endTime: null,
  attachUrl: null,
  pdfAttachUrl: null,
  changeAttachUrl: null,
  changeDescription: null,
  warrantyTime: null,
  purchaseStatus: null,
  id: null,
  contractNumber: null,
  contractName: null,
  signCustomerName: null,
  signCustomerNameName: null,
  address: null,
  amountIncludingTax: null,
  amountExcludingTax: null,
  approvalVersionContract: null,
  resultCause: null,
  virtualStatus: null,
  virtualContractId: null,
  virtualApplyReason: null,
  processInstanceId: null,
  storaged: null,
  result: null,
  plannedPaymentList: null,
  keepPromiseMoneyList: null,
  qualityGuaranteMoneyList: null,
  projectName: null,
  salesManagerStr: null,
}
const form = ref<VirtualContractForm>(initForm);
const triggered = ref<boolean>(false);
function getDetail() {
  if (projectNumber.value) {
    triggered.value = true;
    useContractSignApi().getVirtualByProjectNumber(projectNumber.value).then(res => {
      if (res.code === 0 && res.data) {
        form.value = res.data;
      }
    }).finally(() => {
      triggered.value = false;
    })
  }
}

function getTaxRateType(key: any) {
  if (key === PmContractTaxType.SINGLE ) {
    return '单一税率';
  } else if (key === PmContractTaxType.VARIOUS) {
    return '多种税率';
  } else {
    return '未知'
  }
}

const projectNumber = ref<string>(null);
const processInstanceId = ref<string>(null);
onLoad((query) => {
  projectNumber.value = query['project_number']
  processInstanceId.value = query['process_instance_id']
  getDetail();
})
</script>

<style scoped lang="scss">

</style>
